package com.yuanlrc.hotel.room.controller;

import java.util.List;

import com.yuanlrc.framework.web.domain.server.Sys;
import com.yuanlrc.hotel.rommType.domain.RoomType;
import com.yuanlrc.hotel.rommType.service.IRoomTypeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.yuanlrc.common.annotation.Log;
import com.yuanlrc.common.enums.BusinessType;
import com.yuanlrc.hotel.room.domain.Room;
import com.yuanlrc.hotel.room.service.IRoomService;
import com.yuanlrc.common.core.controller.BaseController;
import com.yuanlrc.common.core.domain.AjaxResult;
import com.yuanlrc.common.utils.poi.ExcelUtil;
import com.yuanlrc.common.core.page.TableDataInfo;

/**
 * 房间管理Controller
 * 
 * @author yuanlrc
 * @date 2020-11-19
 */
@Controller
@RequestMapping("/hotel/room")
public class RoomController extends BaseController {
    private String prefix = "hotel/room";

    @Autowired
    private IRoomService roomService;

    @Autowired
    private IRoomTypeService roomTypeService;

    @RequiresPermissions("hotel:room:view")
    @GetMapping()
    public String room()
    {
        return prefix + "/room";
    }

    /**
     * 查询房间管理列表
     */
    @RequiresPermissions("hotel:room:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Room room)
    {
        startPage();
        List<Room> list = roomService.selectRoomList(room);
        for (Room item: list) {
            item.setRoomType(roomTypeService.selectRoomTypeById(item.getRoomTypeId().longValue()));
        }
        return getDataTable(list);
    }

    /**
     * 导出房间管理列表
     */
    @RequiresPermissions("hotel:room:export")
    @Log(title = "房间管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Room room)
    {
        List<Room> list = roomService.selectRoomList(room);
        ExcelUtil<Room> util = new ExcelUtil<Room>(Room.class);
        return util.exportExcel(list, "room");
    }

    /**
     * 新增房间管理
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        List<RoomType> roomTypes = roomTypeService.selectRoomTypeListAll();
        model.addAttribute("roomType", roomTypes);
        return  "hotel/room/add";
    }

    /**
     * 新增保存房间管理
     */
    @RequiresPermissions("hotel:room:add")
    @Log(title = "房间管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Room room)
    {
        if(room.getRoomTypeId() == null)
        {
            return toAjax(false);
        }

        //根据LIST
        if(roomService.selectRoomByRoomNumber(room.getRoomNumber()) != null)
        {
            return error("房间号已存在");
        }

        int count = roomService.insertRoom(room);

        return toAjax(count);
    }

    /**
     * 修改房间管理
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap, Model model)
    {
        List<RoomType> roomTypes = roomTypeService.selectRoomTypeListAll();
        model.addAttribute("roomType", roomTypes);
        System.out.println(roomTypes.size());
        Room room = roomService.selectRoomById(id);
        room.setRoomType(roomTypeService.selectRoomTypeById(room.getRoomTypeId().longValue()));
        mmap.put("room", room);
        return "hotel/room/edit";
    }

    /**
     * 修改保存房间管理
     */
    @RequiresPermissions("hotel:room:edit")
    @Log(title = "房间管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Room room)
    {
        Room findByRoomNumber = roomService.selectRoomByRoomNumber(room.getRoomNumber());

        if(room != null && room.getId() == null)
        {
            return error("编辑失败");
        }

        //根据房间号来查询
        if(findByRoomNumber != null && findByRoomNumber.getId() != room.getId())
        {
            return error("房间号已存在");
        }

        int count  = 0;
        try
        {
            if(room.getRoomTypeId() == null)
            {
                return toAjax(false);
            }
            count = roomService.updateRoom(room);
        }catch (Exception e)
        {
            return toAjax(false);
        }

        return toAjax(count);
    }

    /**
     * 删除房间管理
     */
    @RequiresPermissions("hotel:room:remove")
    @Log(title = "房间管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        int count = 0;
        try{
            count = roomService.deleteRoomByIds(ids);
        }catch (Exception e)
        {
            return error("删除失败存在关联数据");
        }
        return toAjax(count);
    }
}
